package com.qf.shop.address.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.shop.common.constants.AuthConstants;
import com.qf.shop.common.exception.BusinessException;
import com.qf.shop.common.vo.AddressVo;
import com.qf.shop.common.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.qf.shop.address.entity.AddressEntity;
import com.qf.shop.address.service.AddressService;
import com.qf.shop.common.utils.PageUtils;
import com.qf.shop.common.utils.R;

import javax.servlet.http.HttpSession;


/**
 * @author Java2103
 * @email Java2103@qq.com
 * @date 2021-08-24 10:25:31
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @GetMapping("/getAddressListByUid/{uid}")
    public List<AddressEntity> getAddressListByUid(@PathVariable Integer uid) {
        return addressService.list(new QueryWrapper<AddressEntity>().eq("uid", uid));
    }

    @PostMapping("/insertAddress")
    public R insertAddress(@RequestBody AddressEntity addressEntity, HttpSession session) {
        UserVo userVo = (UserVo) session.getAttribute(AuthConstants.LOGIN_USER);
        if (userVo == null) {
            throw new BusinessException("您还没有登录，请先登录", 002);
        }
        addressEntity.setUid(Integer.parseInt(userVo.getId()));
        addressService.insertAddress(addressEntity);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = addressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public AddressEntity info(@PathVariable("id") Integer id) {
        return addressService.getById(id);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AddressEntity address) {
        addressService.save(address);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AddressEntity address) {
        addressService.updateById(address);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        addressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
